{# Display a table showing all/open/closed tickets. #}

{% extends theme("layout.html") %}
{% set search_form=None %}
{% set title = _("Tickets") %}

{% block content %}
    <h1>{{ _("Tickets") }}</h1>

    {% set status_values = ['all', 'open', 'closed']  %}
    <span>{{ _("Filter:") }}</span>
    {% for status_value in status_values %}
        <form action="{{ url_for('frontend.tickets', selected_tags=list()) }}" method="post" class='moin-ticketsearch-form'>
            <input type="hidden" name="q" value="" >
            <input type="hidden" name="status" value="{{ status_value }}">
            <input type="submit" value="{{ status_value.capitalize() }}" title="{{ _('Show %(status)s tickets', status=status_value) }}" class="{{ 'moin-button active' if status == status_value  else 'moin-button' }}">
        </form>
    {% endfor %}

    <input type="button" class="moin-button ticket-tags-toggle" title="{{ _("Show all tags") }}" value="{{ _("Tags") }}">

    <a href={{ url_for('frontend.ticket_search') }} class="moin-button">{{ _('Advanced Search') }}</a>

    <form class="moin-ticket-search" action="{{ url_for('frontend.tickets',selected_tags=list(selected_tags)) }}" method="post">
        <input type="hidden" name="status" value="{{ status }}">
        <input type="text" name="q" value="{{ query if query }}" id="moin-ticketsearch-query" placeholder="{{ _('Find tickets') }}">
    </form>

    {# TODO: This is a workaround that creates named tickets, rework when there is a means of creating unnamed tickets #}
    <form method="get" class="moin-ticket-create-link" action="{{ url_for('frontend.modify_item', item_name='ticket_'+current_timestamp ) }}">
        <input class="moin-button" type="submit" value="{{ _('Create ticket') }}">
        <input type="hidden" name="itemtype" value="ticket">
    </form>

    <div class="moin-ticket-tags">
        <span>{{ _("Tags:") }}</span>
        {% for tag in tags|sort %}
            {# If user clicks on the tag then it gets selected if it is not already selected #}
            {# If that tag is already selected then it gets deselected #}
            {% set selected_tags_value = list(selected_tags.difference([tag]) if tag in selected_tags else selected_tags.union([tag])) %}
            <form class="moin-existing-tags" action="{{ url_for('frontend.tickets', selected_tags=selected_tags_value) }}" method="post">
                <input type="hidden" name="q" value="{{ query if query }}" >
                <input type="hidden" name="status" value="{{ status }}">
                <input type="submit" value="{{ tag }}" class="{{ 'moin-button active' if tag in selected_tags else 'moin-button' }}">
            </form>
        {% endfor %}
    </div>

    {% if results %}
        <p>{{ _("%(result_len)d Tickets found.", result_len=results|length) }}</p>
        <table class="zebra moin-sortable" id="ticket-list">
            <thead>
                <tr>
                    <th id="ticket-summary">{{ _("Summary") }}</th>
                    <th id="ticket-status">{{ _("Status") }}</th>
                    <th id="ticket-mtime">{{ _("Updated") }}</th>
                    <th id="ticket-assigned-to">{{ _("Assignee") }}</th>
                    <th id="ticket-effort" title="{{ _('Effort') }}">E</th>
                    <th id="ticket-difficulty" title="{{ _('Difficulty') }}">D</th>
                    <th id="ticket-severity" title="{{ _('Severity') }}">S</th>
                    <th id="ticket-priority" title="{{ _('Priority') }}">P</th>
                    <th id="ticket-tags">{{ _("Tags") }}</th>
                </tr>
            </thead>
            <tbody>
                {% for result in results %}
                    <tr>
                        <td>
                            <a href="{{ url_for_item(result['itemid'], field='itemid', namespace=result['namespace']) }}" title="{{ _('ITEMID: %(itemid)s', itemid=result['itemid']) }}">
                                {{ result['summary'] }}
                            </a>
                        </td>
                        <td>
                            {{ _("Closed") if result['closed'] else _("Open") }}
                        </td>
                        <td>
                            {{ result.get('mtime', '').strftime('%Y-%m-%d') }}
                        </td>
                        <td>
                            <a href="{{ url_for('frontend.tickets', assigned_to= get_assigned_to_info(result)) }}">
                                {{ get_assigned_to_info(result) }}
                            </a>
                        </td>
                        <td>
                            {{ result.get('effort', '') }}
                        </td>
                        <td>
                            {{ result.get('difficulty', '') }}
                        </td>
                        <td>
                            {{ result.get('severity', '') }}
                        </td>
                        <td>
                            {{ result.get('priority', '') }}
                        </td>
                        <td>
                            {% for tag in result['tags'] %}
                                {{ tag }}
                            {% endfor %}
                        </td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>

        <p id="ticket-table-tip">
            {{ _("Tip: Sort multiple columns simultaneously by holding down the Shift key and clicking on additional column headers!") }}
        </p>
    {% else %}
        <p>
            {{ _("No tickets found.") }}
        </p>
    {% endif %}

{% endblock %}
{% block body_scripts %}
    {{ super() }}
    <script src="{{ url_for('static', filename='js/tickets.js') }}"></script>
{% endblock %}
